/*
 * LDIR
 *
 * LDIR, a GPS cartography application built for Let's Do It Romania eco
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 *
 * http://generasoftware.com/
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "wasteinfo.h"
#include "WsObject.h"
#include "WSUtilities.h"
#include "Json.h"
#include "Globals.h"
#include <fieldsstringtable.h>

CWasteInfo::CWasteInfo()
    {
    // No implementation required
    }

CWasteInfo::~CWasteInfo()
    {
    iCounty.Close();
    iComment.Close();
    }

CWasteInfo* CWasteInfo::NewLC()
    {
    CWasteInfo* self = new ( ELeave ) CWasteInfo();
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

CWasteInfo* CWasteInfo::NewL()
    {
    CWasteInfo* self = CWasteInfo::NewLC();
    CleanupStack::Pop(); // self;
    return self;
    }

void CWasteInfo::ConstructL()
    {
    iId.Copy( GenerateRandom() );
#ifdef __TEST__
    iComment .CreateL( _L( "test aplicatii mobile" ) );
#endif
    }

void CWasteInfo::ExternalizeL( RWriteStream& aStream ) const
    {
    aStream.WriteInt32L( iId.Length() );
    if ( iId.Length() )
        {
        aStream.WriteL( iId, iId.Length() );
        }
    aStream.WriteReal32L( iLongitude );
    aStream.WriteReal32L( iLatitude );
    aStream.WriteInt32L( iCounty.Length() );
    if ( iCounty.Length() )
        {
        aStream.WriteL( iCounty, iCounty.Length() );
        }
    aStream.WriteInt32L( iDiameter );
    aStream.WriteInt32L( iLength );
    aStream.WriteInt32L( iWidth );
    aStream.WriteInt32L( iHeight );
    aStream.WriteInt32L( iBagEq );
    aStream.WriteInt32L( iIsDispersed );
    aStream.WriteInt32L( iRoadAccess );
    aStream.WriteInt32L( iRainAccess );
    aStream.WriteInt32L( iUnreciclable );
    aStream.WriteInt32L( iGlass );
    aStream.WriteInt32L( iMetal );
    aStream.WriteInt32L( iPlastic );
    aStream.WriteInt32L( iCardboard );
    aStream.WriteInt32L( iRubber );
    aStream.WriteInt32L( iOversized );
    aStream.WriteInt32L( iConstruction );
    aStream.WriteInt32L( iExcluded );
    aStream.WriteInt32L( iComment.Length() );
    if ( iComment.Length() )
        {
        aStream.WriteL( iComment, iComment.Length() );
        }
    aStream.WriteInt32L( iTime.DateTime().Year() );
    aStream.WriteInt32L( iTime.DateTime().Month() );
    aStream.WriteInt32L( iTime.DateTime().Day() );
    aStream.WriteInt32L( iTime.DateTime().Hour() );
    aStream.WriteInt32L( iTime.DateTime().Minute() );
    aStream.WriteInt32L( iTime.DateTime().Second() );
    }

void CWasteInfo::InternalizeL( RReadStream& aStream )
    {
    TInt length = aStream.ReadInt32L();
    if ( length )
        {
        aStream.ReadL( iId, length );
        }
    iLongitude = aStream.ReadReal32L();
    iLatitude = aStream.ReadReal32L();
    length = aStream.ReadInt32L();
    if ( length )
        {
        iCounty.Close();
        iCounty.CreateMaxL( length );
        aStream.ReadL( iCounty, length );
        }
    iDiameter = aStream.ReadInt32L();
    iLength = aStream.ReadInt32L();
    iWidth = aStream.ReadInt32L();
    iHeight = aStream.ReadInt32L();
    iBagEq = aStream.ReadInt32L();
    iIsDispersed = aStream.ReadInt32L();
    iRoadAccess = aStream.ReadInt32L();
    iRainAccess = aStream.ReadInt32L();
    iUnreciclable = aStream.ReadInt32L();
    iGlass = aStream.ReadInt32L();
    iMetal = aStream.ReadInt32L();
    iPlastic = aStream.ReadInt32L();
    iCardboard = aStream.ReadInt32L();
    iRubber = aStream.ReadInt32L();
    iOversized = aStream.ReadInt32L();
    iConstruction = aStream.ReadInt32L();
    iExcluded = aStream.ReadInt32L();
    length = aStream.ReadInt32L();
    if ( length )
        {
        iComment.Close();
        iComment.CreateMaxL( length );
        aStream.ReadL( iComment, length );
        }
    else
        {
        iComment.Close();
        iComment.CreateL( _L( "test aplicatii mobile" ) );
        }
    TInt val = aStream.ReadInt32L();
    TDateTime time;
    time.SetYear( val );
    val = aStream.ReadInt32L();
    time.SetMonth( ( TMonth ) val );
    val = aStream.ReadInt32L();
    time.SetDay( val );
    val = aStream.ReadInt32L();
    time.SetHour( val );
    val = aStream.ReadInt32L();
    time.SetMinute( val );
    val = aStream.ReadInt32L();
    time.SetSecond( val );
    iTime = TTime( time );
    }

HBufC8* CWasteInfo::EncodeL( CJsonGenerator& aJsonGenerator )
    {

    ExternalizeToEntityL();

    HBufC8* retBuff = CWsObject::EncodeL( aJsonGenerator );
    delete iEntity;
    iEntity = NULL;
    return retBuff;
    }

void CWasteInfo::DecodeL( const TDesC8& aDesC, CJsonParser& aJsonParser )
    {
    CWsObject::DecodeL( aDesC, aJsonParser );

    //    CEntity* entity = NULL;
    CPair* pair = NULL;
    CEntity* pairValue = NULL;
    TInt index = 0;

    delete iEntity;
    iEntity = NULL;
    }

void CWasteInfo::ExternalizeToEntityL()
    {
    _LIT8( KFormatDateTime, "/Date(%S)/" );
    delete iEntity;
    iEntity = NULL;
    iEntity = CEntity::NewL( obj_type );

    AddIntAttributeL( FieldsStringTable::EFldBagEq, iBagEq );
    AddDesAttributeL( FieldsStringTable::EFldComment, iComment );
    AddIntAttributeL( FieldsStringTable::EFldDiameter, iDiameter );
    AddIntAttributeL( FieldsStringTable::EFldHeight, iHeight );
    AddDesAttributeL( FieldsStringTable::EFldId, iId );
    AddBoolAttributeL( FieldsStringTable::EFldRoadAccess, iRoadAccess );
    AddBoolAttributeL( FieldsStringTable::EFldRainAccess, iRainAccess );
    AddBoolAttributeL( FieldsStringTable::EFldIsDispersed, iIsDispersed );
    AddBoolAttributeL( FieldsStringTable::EFldExcluded, iExcluded );
    AddIntAttributeL( FieldsStringTable::EFldLength, iLength );

    CEntity* locationEntity = CEntity::NewLC( obj_type );
    // add latitude
    CEntity* child = CEntity::NewLC( pair_type );
    CEntity* pairValue = CEntity::NewLC( real_type );
    pairValue->SetValueL( iLatitude );
    CPair* pair = CPair::NewL( FieldsStringTable::EFldLatitude, pairValue );
    CleanupStack::Pop( pairValue );
    CleanupStack::PushL( pair );
    child->SetValueL( pair );
    CleanupStack::Pop( pair );
    locationEntity->AddChildL( child );
    CleanupStack::Pop( child );
    // add longitude
    child = CEntity::NewLC( pair_type );
    pairValue = CEntity::NewLC( real_type );
    pairValue->SetValueL( iLongitude );
    pair = CPair::NewL( FieldsStringTable::EFldLongitude, pairValue );
    CleanupStack::Pop( pairValue );
    CleanupStack::PushL( pair );
    child->SetValueL( pair );
    CleanupStack::Pop( pair );
    locationEntity->AddChildL( child );
    CleanupStack::Pop( child );
    // add to object
    AddEntityL( FieldsStringTable::EFldLocation, *locationEntity );
    CleanupStack::PopAndDestroy( locationEntity );
    AddIntAttributeL( FieldsStringTable::EFldCardboard, iCardboard );
    AddIntAttributeL( FieldsStringTable::EFldGlass, iGlass );
    AddIntAttributeL( FieldsStringTable::EFldMetal, iMetal );
    AddIntAttributeL( FieldsStringTable::EFldUnreciclable, iUnreciclable );
    AddIntAttributeL( FieldsStringTable::EFldPlastic, iPlastic );
    AddIntAttributeL( FieldsStringTable::EFldConstruction, iConstruction );
    AddIntAttributeL( FieldsStringTable::EFldRubber, iRubber );
    AddIntAttributeL( FieldsStringTable::EFldOversized, iOversized );
    TBuf8<32> datetime;
    TBuf8<16> timestamp( GetTimestamp( iTime ) );
    datetime.Format( KFormatDateTime, &timestamp );
    AddDes8AttributeL( FieldsStringTable::EFldDateTime, datetime );
    //AddDesAttributeL( FieldsStringTable::EFldCounty, iCounty );
    AddIntAttributeL( FieldsStringTable::EFldWidth, iWidth );
    }

CWasteInfo* CWasteInfo::CloneL()
    {
    CWasteInfo* wasteInfo = CWasteInfo::NewLC();
    wasteInfo->iId = iId;
    wasteInfo->iLongitude = iLongitude;
    wasteInfo->iLatitude = iLatitude;
    wasteInfo->iCounty.Close();
    wasteInfo->iCounty.CreateL( iCounty );
    wasteInfo->iDiameter = iDiameter;
    wasteInfo->iLength = iLength;
    wasteInfo->iWidth = iWidth;
    wasteInfo->iHeight = iHeight;
    wasteInfo->iBagEq = iBagEq;
    wasteInfo->iIsDispersed = iIsDispersed;
    wasteInfo->iRoadAccess = iRoadAccess;
    wasteInfo->iRainAccess = iRainAccess;
    wasteInfo->iUnreciclable = iUnreciclable;
    wasteInfo->iGlass = iGlass;
    wasteInfo->iMetal = iMetal;
    wasteInfo->iPlastic = iPlastic;
    wasteInfo->iCardboard = iCardboard;
    wasteInfo->iRubber = iRubber;
    wasteInfo->iOversized = iOversized;
    wasteInfo->iConstruction = iConstruction;
    wasteInfo->iExcluded = iExcluded;
    wasteInfo->iComment.Close();
    wasteInfo->iComment.CreateL( iComment );
    wasteInfo->iTime = iTime;

    CleanupStack::Pop( wasteInfo );
    return wasteInfo;
    }

